﻿using UnityEngine;
using System.Collections;
namespace UnityExtension
{
	public static class TransformUtility
	{
		/// <summary>
		/// 递归查找某个名字的Transform
		/// </summary>
		/// <param name="trans"></param>
		/// <param name="name"></param>
		/// <returns></returns>
		public static Transform FindChildByName(this Transform trans,string name)
		{
			Transform t = null;
			if(trans.name == name)
			{
				return trans;
			}
			for(int i = 0,length = trans.childCount;i < length;i++)
			{
				t = trans.GetChild(i).FindChildByName(name);
				if(t != null)
				{
					return t;
				}
			}
			return t;
		}
		public static T GetComponentOrAdd<T>(this Transform trans) where T:Component,new()
		{
			return trans.gameObject.GetComponentOrAdd<T>();
		}
	}
}
